import { createElm } from './createElm.js';
import { updateChildren } from './updateChildren.js';
/* 新旧虚拟节点比较 */
function patchVnode (oldVnode, newVnode) {
  if (oldVnode === newVnode) return;
  /* 判断新虚拟节点有没有text属性，且children为空 */
  if (newVnode.text !== undefined && (newVnode.children == undefined || newVnode.children.length == 0)) {
    /* 判断oldVnode和newVnoded的text是否相同 */
    if (newVnode.text != oldVnode.text) {
      oldVnode.elm.innerText = newVnode.text;
    }
  } else {
    if (oldVnode.children !== undefined && newVnode.children.length > 0) {
      updateChildren(oldVnode.elm, oldVnode.children, newVnode.children);
    } else {
      /* oldVnode没有children属性 */
      /* 清空oldVnode的text，并且把newVnode的Children添加到DOM中 */
      oldVnode.elm.innerText = '';
      for (let i = 0; i < newVnode.children.length; i++) {
        let newVnodeElm = createElm(newVnode.children[i]);
        oldVnode.elm.appendChild(newVnodeElm);
      }
    }
  }
}

export {
  patchVnode
}